vim.g.mapleader = " "

local keymap = vim.keymap

-- ---------- 插入模式 ---------- ---
keymap.set("i", "jj", "<ESC>")
keymap.set("c", "jj", "<ESC>")

-- ---------- 视觉模式 ---------- ---
-- 单行或多行移动
keymap.set("v", "J", ":m '>+1<CR>gv=gv")
keymap.set("v", "K", ":m '<-2<CR>gv=gv")

-- ---------- 正常模式 ---------- ---
-- 窗口
keymap.set("n", "<leader>sv", "<C-w>v") -- 水平新增窗口 
keymap.set("n", "<leader>sh", "<C-w>s") -- 垂直新增窗口
-- keymap.set("n", "<leader>sw", ":q<CR>") -- 关闭当前窗口
keymap.set("n", "<leader>sw", ":BufferLinePickClose<CR>", {noremap = true, silent = true})  -- 关闭某一个 buffer

-- 终端
-- keymap.set("t", "<ESC>", "<C-\\><C-n>", {noremap = true, silent = true}) -- 在终端模式中按esc回到普通模式
-- keymap.set("n", "<A-=>", ":split term://zsh<CR>", {noremap = true, silent = true})
-- keymap.set("t", "<A-=>", "<C-\\><C-n>:bdelete! %<CR>", , {noremap = true, silent = true})
keymap.set("t", "<Esc>", "<C-\\><C-n>", {noremap = true, silent = true}) 
keymap.set("t", "<C-A-L>", "<Cmd> wincmd l<CR>", {noremap = true, silent = true}) 
keymap.set("t", "<C-A-H>", "<Cmd> wincmd h<CR>", {noremap = true, silent = true}) 
keymap.set("t", "<C-A-J>", "<Cmd> wincmd j<CR>", {noremap = true, silent = true}) 
keymap.set("t", "<C-A-k>", "<Cmd> wincmd k<CR>", {noremap = true, silent = true})

keymap.set("n", "<leader>lg", "<Cmd>lua lazygit_toggle()<CR>", {noremap = true, silent = true})

-- 配置文件
keymap.set("n", "<leader>se", ":vs $MYVIMRC<CR>", {silent = true, noremap = true})  -- 打开init.vim
keymap.set("n", "<leader>sr", ":source $MYVIMRC<CR>", {silent = true, noremap = true}) -- 不退出加载

-- 取消高亮
keymap.set("n", "<leader>nh", ":nohl<CR>")

-- Diffview
keymap.set("n", "<leader>do", ":DiffviewOpen<CR>", {silent = true, noremap = true})  -- 打开Diffview
keymap.set("n", "<leader>dc", ":DiffviewClose<CR>", {silent = true, noremap = true})  -- 关闭Diffview

-- 切换buffer
keymap.set("n", "<C-A-L>", ":bnext<CR>")
keymap.set("n", "<C-A-H>", ":bprevious<CR>")
keymap.set("n", "<C-A-w>", ":bdelete %<CR>", {noremap = true})  -- 关闭当前buffer


keymap.set("n", "<leader>1", ":BufferLineGoToBuffer 1<CR>", {noremap = true, silent = true})
keymap.set("n", "<leader>2", ":BufferLineGoToBuffer 2<CR>", {noremap = true, silent = true})
keymap.set("n", "<leader>3", ":BufferLineGoToBuffer 3<CR>", {noremap = true, silent = true})
keymap.set("n", "<leader>4", ":BufferLineGoToBuffer 4<CR>", {noremap = true, silent = true})
keymap.set("n", "<leader>5", ":BufferLineGoToBuffer 5<CR>", {noremap = true, silent = true})
keymap.set("n", "<leader>6", ":BufferLineGoToBuffer 6<CR>", {noremap = true, silent = true})
keymap.set("n", "<leader>7", ":BufferLineGoToBuffer 7<CR>", {noremap = true, silent = true})
keymap.set("n", "<leader>8", ":BufferLineGoToBuffer 8<CR>", {noremap = true, silent = true})
keymap.set("n", "<leader>9", ":BufferLineGoToBuffer 9<CR>", {noremap = true, silent = true})
keymap.set("n", "<leader>bg", ":BufferLinePick", {noremap = true, silent = true})

-- ---------- 插件 ---------- ---
-- nvim-tree
keymap.set("n", "<leader>e", ":NvimTreeToggle<CR>")


-- Telescope
vim.keymap.set('n', '<leader>ff', builtin.find_files, {})
vim.keymap.set('n', '<leader>fg', builtin.live_grep, {})  -- 环境里要安装ripgrep
vim.keymap.set('n', '<leader>fb', builtin.buffers, {})
vim.keymap.set('n', '<leader>fh', builtin.help_tags, {})
vim.keymap.set("n", "<leader>fm", "<Cmd>Telescope marks<CR>", {noremap = true, silent = true})
vim.keymap.set("n", "<leader>fj", "<Cmd>Telescope jumplist<CR>", {noremap = true, silent = true})
vim.keymap.set('n', '<leader>fp', ":lua require'telescope'.extensions.project.project{}<CR>", {noremap = true, silent = true})

vim.keymap.set("n", "<leader>sp", ":Telescope xray23 list<CR>")
vim.keymap.set("n", "<leader>ss", ":Telescope xray23 save<CR>")


-- ----------------- LuaSnip keymap ---------------
-- 默认用Tab/Shift-tab选择，A-S-E 确定，C-e 中止，在snip里面也用tab/Shift-tab补全。
-- snippet文件

vim.keymap.set({"i"}, "<A-w>", function() require("luasnip").expand() end, {silent = true})
